﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MediaPortal.Configuration;

namespace MyEmulators2
{
    class Conf_GlobalSettings : IDisposable
    {
        Conf_GlobalSettings()
        {
            initThumbDir();
        }

        void initThumbDir()
        {
            string location = Options.Instance.GetStringOption("thumblocation");
            if (location == "")
                location = Config.GetFolder(Config.Dir.Thumbs);

            if (!System.IO.Directory.Exists(location))
            {
                Logger.LogError("Unable to locate thumb folder '{0}', reverting to default thumb location", location);
                location = Config.GetFolder(Config.Dir.Thumbs); //default to MP thumb directory
                Options.Instance.UpdateOption("thumblocation", location);
            }

            if (location.EndsWith("\\"))
                location = location.Substring(0, location.Length - 1); //remove any trailing '\'

            ThumbDirectory = location;

            DefaultLogo = MediaPortal.GUI.Library.GUIGraphicsContext.Skin + "\\media\\defaultEmulators2Logo.png";
            DefaultFanart = MediaPortal.GUI.Library.GUIGraphicsContext.Skin + "\\media\\defaultEmulators2Fanart.png";
        }

        bool isConfig = false;
        public bool IsConfig
        {
            get { return isConfig; }
            set { isConfig = value; }
        }

        public string ThumbDirectory
        {
            get;
            private set;
        }

        public string DefaultLogo
        {
            get;
            private set;
        }

        public string DefaultFanart
        {
            get;
            private set;
        }

        public void ReInit()
        {
            importer = new Importer();
            if (thumbContextMenu != null)
            {
                thumbContextMenu.Dispose();
                thumbContextMenu = null;
            }
        }

        static object instanceLock = new object();
        static Conf_GlobalSettings instance = null;
        public static Conf_GlobalSettings Instance
        {
            get
            {
                if (instance == null)
                    lock (instanceLock)
                        if (instance == null)
                            instance = new Conf_GlobalSettings();
                return instance;
            }
        }

        Importer importer = null;
        public Importer Importer
        {
            get
            {
                if (importer == null)
                    importer = new Importer();
                return importer;
            }
        }

        ThumbContext thumbContextMenu = null;
        public ThumbContext ThumbContextMenu
        {
            get
            {
                if (thumbContextMenu == null)
                    thumbContextMenu = new ThumbContext();
                return thumbContextMenu;
            }
        }

        public static OpenFileDialog OpenFileDialog(string title, string filter, string initialDirectory)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            dlg.Title = title;
            dlg.Filter = filter;
            dlg.InitialDirectory = initialDirectory;

            dlg.CheckFileExists = true;
            dlg.CheckPathExists = true;
            dlg.ValidateNames = true;

            dlg.Multiselect = false;
            dlg.RestoreDirectory = true;

            return dlg;
        }

        public static FolderBrowserDialog OpenFolderDialog(string title, string initialDirectory)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            dlg.Description = title;
            dlg.SelectedPath = initialDirectory;
            return dlg;
        }

        public void ShowMPDialog(string message, params object[] args)
        {
            message = string.Format(message, args);
            string heading = Options.Instance.GetStringOption("shownname");

            if (isConfig)
            {
                MessageBox.Show(message, heading, MessageBoxButtons.OK);
                return;
            }

            MediaPortal.Dialogs.GUIDialogOK dlg_error = (MediaPortal.Dialogs.GUIDialogOK)MediaPortal.GUI.Library.GUIWindowManager.GetWindow
                ((int)MediaPortal.GUI.Library.GUIWindow.Window.WINDOW_DIALOG_OK);
            if (dlg_error != null)
            {
                dlg_error.Reset();
                dlg_error.SetHeading(heading);
                
                message = "\r\n" + message;
                dlg_error.SetLine(1, message);
                dlg_error.DoModal(MediaPortal.GUI.Library.GUIWindowManager.ActiveWindow);
            }
        }

        Conf_ThumbRetriever thumbRetriever = null;
        public void ShowThumbRetriever(Game game)
        {
            if (thumbRetriever == null)
                thumbRetriever = new Conf_ThumbRetriever(game);
            else
                thumbRetriever.Reset(game);

            if (thumbRetriever.Visible)
                thumbRetriever.Activate();
            else
                thumbRetriever.Show();
        }

        public void CloseThumbRetriever()
        {
            if (thumbRetriever != null)
            {
                thumbRetriever.close();
                thumbRetriever = null;
            }
        }

        public void Dispose()
        {
            ReInit();
        }
    }
}
